//
//  XJHomeViewController.swift
//  XJZB
//
//  Created by 李胜兵 on 16/9/23.
//  Copyright © 2016年 付公司. All rights reserved.
//

import UIKit
private let kTitltViewH :CGFloat = 40



class XJHomeViewController: UIViewController {

    // MARK: - 懒加载标题视图
    fileprivate lazy var pageTitleView : XJPageTitleView = { [weak self] in
        let titleFrame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH, width: kScreenW, height: kTitltViewH)
        let titles = ["推荐","游戏","娱乐","趣玩"]
        let titleView = XJPageTitleView(frame: titleFrame, titles: titles)
        titleView.delegate = self
        return titleView
    }()
    
    // MARK: - 懒加载内容视图
    fileprivate lazy var pageContentView : XJPageContentView =  { [weak self] in
        // 1.确定内容视图的frame
       let contentH = kScreenH - kStatusBarH - kNavigationBarH - kTitltViewH - kTabBarH
       let contentFrame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH + kTitltViewH, width: kScreenW, height: contentH)
        
        // 2.确定所有子控制器
        var childVcs = [UIViewController]()
        childVcs.append(XJRecommentViewController())
        childVcs.append(XJGameViewController())
        childVcs.append(XJAmuseViewController())
        childVcs.append(XJFunnyViewController())
        
       let contentView = XJPageContentView(frame: contentFrame, childVcs: childVcs, parentViewController: self)
        contentView.delegate = self
        return contentView
    }()
    
    
    // MARK: - 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        // MARK: - 设置UI
        setupUI()
        
    }
}





// MARK: - 设置UI界面
extension XJHomeViewController {
    fileprivate func setupUI() {
        // 0.不需要调整UIScrollView的内边距
        automaticallyAdjustsScrollViewInsets = false
        
        // 2.添加TitleView
        view.addSubview(pageTitleView)
        
        // 3.添加contentView
        view.addSubview(pageContentView)
        
    }
}


// MARK: - 遵守XJPageTitleViewDelegate代理协议
extension XJHomeViewController : XJPageTitleViewDelegate {
    func xjPageTitleView(_ titleView: XJPageTitleView, selectedIndex index: Int) {
        pageContentView.setCurrentIndex(index)
    }
}


// MARK: - 遵守XJPageContentViewDelegate代理协议
extension XJHomeViewController : XJPageContentViewDelegate {
    func xjPageContentView(_ contentView: XJPageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleView.setTitleViewWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}











